<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>我的地址</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <link rel="stylesheet" type="text/css" href="../css/swiper.min.css" />
    <link rel="stylesheet" type="text/css" href="../css/layer.css" />
    <link rel="stylesheet" type="text/css" href="../css/mdw.css" />
    <script type="text/javascript" src="../script/api.js"></script>
    <script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../script/swiper.min.js"></script>
    <script type="text/javascript" src="../script/mobile.js"></script>
    <script type="text/javascript" src="../script/layer.js"></script>
    <script type="text/javascript" src="../script/mdw.js"></script>
    <script type="text/javascript" src="../script/jumpPage.js"></script>
</head>
<body class="bg-eee">
    <header class="header">
        <div class="backPrevPage" onclick="historyBack()"></div>
        <div class="pageTitle">我的地址</div>
    </header>
    <section class="addressPage HTML_BOX">
        <div class="pageTitle plr displayFlex">
            <div class="add_title">账单地址</div>
            <div id="zdadd" class="btn" onclick="adddz('1')" >添加</div>
        </div>
        <ul id="zdlist" class="addressList bg-eee">
            <!-- <li id="add_1" class="plr">
                <div class="addressBox">
                    <div><span>张小二</span><span>18223450595</span></div>
                    <div class="addressInfo">重庆市南岸区万</div>
                </div>
                <div class="addressBox2 displayFlex">
                    <div class="default_address "><i></i>设为默认</div>
                    <div>
                        <i class="icon1"></i>
                       <i data-id="1" class="icon2 delete"></i>
                    </div>
                </div>
            </li>
            <li class="plr">
                <div class="addressBox">
                    <div><span>张小二</span><span>18223450595</span></div>
                    <div class="addressInfo">重庆市南岸区万</div>
                </div>
                <div class="addressBox2 displayFlex">
                    <div class="default_address "><i></i>设为默认</div>
                    <div>
                        <i class="icon1"></i>
                        <i data-id="1" class="icon2 delete"></i>
                    </div>
                </div>
            </li> -->
        </ul>
        <div class="pageTitle plr displayFlex">
            <div class="add_title">收货地址</div>
            <div class="btn" onclick="adddz('2')" >添加</div>
        </div>
        <ul id="shlist" class="addressList bg-eee">
            <!-- <li class="plr">
                <div class="addressBox">
                    <div><span>张小二</span><span>18223450595</span></div>
                    <div class="addressInfo">重庆市南岸区万</div>
                </div>
                <div class="addressBox2 displayFlex">
                    <div class="default_address on"><i></i>设为默认</div>
                    <div>
                        <i class="icon1"></i>
                        <i class="icon2"></i>
                    </div>
                </div>
            </li> -->
            
        </ul>
    </section>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    var param = {};
    apiready = function(){
        $api.fixStatusBar($api.dom('header'));
        api.setStatusBarStyle({
            color: '#fff',//设置APP状态栏背景色
            style: 'dark'
        });
        api.addEventListener({
            name: 'add_address'
        }, function(ret, err) {
            $("#zdlist").empty();
            $("#shlist").empty();
            ready();
        });
        ifCnAndEn();
        param = api.pageParam;
        ready();
    };

    function ready(){
        pop.loading();
        getAjax(Interface.address_list,{token:token,type:1},function(res){
            console.log(JSON.stringify(res));
            pop.close();
            var zd = res.data.zdaddress;
            var sh = res.data.shaddress;
            if(zd.address){
                $("#zdadd").hide();
                var html = '';
                if(zd.name==null){
                    zd.name ="";
                }
                if(!zd.city_name){
                    zd.city_name = "";
                }
                if(!zd.other_cities){
                    zd.other_cities = zd.city_name;
                }
                if(zd.other_cities){
                    zd.other_cities = ", "+zd.other_cities;
                }
                html +='<li id="add_'+zd.id+'" class="plr">';
                     html +='<div class="addressBox">';
                        html +='<div><span  class="mr5">'+zd.username+'&nbsp;'+zd.name+'</span><span>'+zd.mobile+'</span></div>';
                        html +='<div class="addressInfo">'+zd.address+zd.gongyu+zd.other_cities+"<br/>"+zd.province_name+", "+zd.country_name+" "+zd.you_code+'</div>';
                    html +='</div>';
                    html +='<div class="addressBox4 ">';
                      //  html +='<div class="default_address "><i></i>设为默认</div>';
                        html +='<div><i onclick="adddz(1,'+zd.id+')" class="icon1"></i><i onclick="deletes('+zd.id+',1)" class="icon2"></i>';
                        html +='</div>';
                    html +='</div>';
                html +='</li>';
                $("#zdlist").append(html);
            }
           
            if(sh){
                $.each(sh,function(i,item){
                var html = '';
                if(item.name==null){
                    item.name ="";
                }
                if(!item.city_name){
                    item.city_name = "";
                }
                if(!item.other_cities){
                    item.other_cities = item.city_name;
                }
                if(item.other_cities){
                    item.other_cities = ", "+item.other_cities;
                }
                html +='<li id="add_'+item.id+'" class="plr">';
                     html +='<div class="addressBox">';
                        html +='<div><span class="mr5">'+item.username+" "+item.name+'</span><span>'+item.mobile+'</span></div>';
                        html +='<div class="addressInfo">'+item.address+item.gongyu+item.other_cities+"<br/>"+item.province_name+", "+item.country_name+" "+item.you_code+'</div>';
                    html +='</div>';
                    html +='<div class="addressBox2 displayFlex">';
                        var it = '设为默认';
                        if(param.id==1){
                            it='确定';
                        }
                        if(cnen == 2){
                            it='set as default';
                            if(param.id==1){
                                it='Confirm';
                            }

                        }
                        moren = "";
                        if(item.is_moren == 1){
                           moren = "on"; 
                        }
                        html +='<div onclick="defaults(this,'+item.id+')" class="default_address sh_address '+moren+'"><i></i>'+it+'</div>';
                        html +='<div><i onclick="adddz(2,'+item.id+')" class="icon1"></i><i class="icon2 " onclick="deletes('+item.id+',2)"></i>';
                        html +='</div>';
                    html +='</div>';
                html +='</li>';
                $("#shlist").append(html);
            })
            }
        },function(res,err){})
    }
    
    function defaults (e,id){

        pop.loading();
        var p = {token:token,address_id:id};
        console.log(JSON.stringify(p));
        getAjax(Interface.moren_address,p,function(res){
            pop.close();
            $(".default_address").removeClass("on");
            $(e).addClass("on");
            if(param.id == 1){
                api.sendEvent({
                    name: 'order_address',
                    extra: {id:id}
                });
                historyBack();
            }
        },function(res,err){})
    }
   

    function adddz(type,id){
        var p = {type:type,id:id};//1为 新增订单地址，2位新增收货地址，
        openWin('address_add','address_add.html',p)
    }

    function deletes(id,type){

        pop.loading();
        getAjax(Interface.del_address,{token:token,address_id:id},function(res){
            pop.notice(res.msg);
            pop.close();
            deleteElement( $("#add_"+id));
            api.sendEvent({
                name: 'order_address',
                extra: {id:id}
            });
            if(type==1){
                $("#zdadd").show();
            }
        },function(res,err){})
    }
</script>
<script type="text/javascript">
    function ifCnAndEn(){
        if(cnen==2){
           $(".pageTitle").eq(0).html('My Address');
           $(".btn").html('Add');
           $(".add_title").eq(0).html('Billing Address');
           $(".add_title").eq(1).html('Delivery Address');
        }
    }
</script>
</html>